﻿using System.Globalization;
using System.Text.RegularExpressions;

namespace ContactSMS.Commons.Util
{
    public class TimeUtil
    {
        public static long GetTimestamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            int timestamp = (int)ts.TotalSeconds;
            return timestamp;
        }

        public static long GetMkTime(string dtime)
        {
            if (!Regex.IsMatch(dtime, "[^0-9]"))
            {
                return long.Parse(dtime);
            }

            dtime = dtime.Trim();
            string[] invalidChars = { "\r", "\n", "\t", "日", "秒" };
            foreach (var ch in invalidChars)
            {
                dtime = dtime.Replace(ch, " ");
            }

            dtime = dtime.Replace("年", "-");
            dtime = dtime.Replace("月", "-");
            dtime = dtime.Replace("时", ":");
            dtime = dtime.Replace("分", ":");
            dtime = Regex.Replace(dtime, @"\s+", " ").Trim();

            string[] ds = dtime.Split(' ');
            string[] ymd = ds.Length > 0 ? ds[0].Split('-') : new string[] { };

            if (ymd.Length == 1)
            {
                ymd = ds[0].Split('.');
            }

            int[] dt = new int[] { 1970, 1, 1, 0, 0, 0 };

            if (ymd.Length > 0 && ymd[0].Length == 2)
            {
                dt[0] = int.Parse("20" + ymd[0]);
            }
            else if (ymd.Length > 0)
            {
                dt[0] = int.Parse(ymd[0]);
            }

            if (ymd.Length > 1) dt[1] = int.Parse(ymd[1]);
            if (ymd.Length > 2) dt[2] = int.Parse(ymd[2]);

            if (ds.Length > 1)
            {
                string[] hms = ds[1].Split(':');
                if (hms.Length > 0) dt[3] = int.Parse(hms[0]);
                if (hms.Length > 1) dt[4] = int.Parse(hms[1]);
                if (hms.Length > 2) dt[5] = int.Parse(hms[2]);
            }

            for (int i = 0; i < dt.Length; i++)
            {
                if (dt[i] == 0)
                {
                    continue;
                }
                var v = dt[i].ToString().TrimStart('0');
                if (string.IsNullOrEmpty(v))
                {
                    dt[i] = 0;
                }
                else
                {
                    dt[i] = int.Parse(v);
                }
            }

            DateTime dateTime;
            if (DateTime.TryParseExact(
                $"{dt[0]}-{dt[1]}-{dt[2]} {dt[3]}:{dt[4]}:{dt[5]}",
                "yyyy-MM-dd HH:mm:ss",
                CultureInfo.InvariantCulture,
                DateTimeStyles.None,
                out dateTime))
            {
                return (long)dateTime.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
            }
            else
            {
                return (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
            }
        }
    }
}
